package com.seio.edc.controller.system;

import com.nova.edc.model.system.*;
import com.nova.edc.service.system.*;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/systemAreaPoint")
public class AreaPointContronller {

    @Autowired
    private DeviceTypeService novaDeviceTypeService;

    @Autowired
    private AreaPointService novaAreaPointService;

    @Autowired
    private CustomerAreaService novaCustomerAreaService;

    @Autowired
    private com.nova.edc.service.system.CustomerInfoService novaCustomerInfoService;

    private static final int PAGE_SIZE = 10;

    /**
     * 初始化节点管理
     *
     * @return
     */
    @RequestMapping(value = "areaPointList")
    public ModelAndView areaPointList(HttpServletRequest request,
                                      HttpSession session, Integer pageIndex, AreaPoint areaPoint, ModelMap modelMap) {
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        CustomerArea customerArea = novaCustomerAreaService.findById(areaPoint.getAreaid());
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerArea.getCustomerinfoid());
        PageList<AreaPoint> pageAreaPoint = novaAreaPointService.findAreaPointPageList(areaPoint, page, "gatewayid,sensorid,channelid", "asc");
        modelMap.put("customerInfo", customerInfo);
        modelMap.put("customerArea", customerArea);
        modelMap.put("areaPoint", areaPoint);
        modelMap.put("AreaPoints", pageAreaPoint.getDatas());
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        return new ModelAndView("system/areaPointList", modelMap);
    }

    //初始化节点编辑页面
    @RequestMapping(value = "areaPointEdit")
    public ModelAndView areaPointEdit(HttpServletRequest request,
                                      HttpSession session, Integer id, Integer areaid,
                                      ModelMap modelMap) {
        if (id != null && id != 0) {
            AreaPoint areapoints = novaAreaPointService.findById(id);
            modelMap.put("areapoints", areapoints);
        }
        List<DeviceType> deviceType = novaDeviceTypeService.findLightAir();
        CustomerArea customerArea = novaCustomerAreaService.findById(areaid);
        modelMap.put("deviceType", deviceType);
        modelMap.put("customerArea", customerArea);
        return new ModelAndView("system/areaPointEdit", modelMap);
    }

    //编辑确认
    @RequestMapping(value = "saveAreaPoint")
    public ModelAndView saveAreaPoint(HttpServletRequest request,
                                      HttpSession session, AreaPoint areaPoint,
                                      ModelMap modelMap) {
        novaAreaPointService.fortrim(areaPoint);
        if (areaPoint.getId() != null && areaPoint.getId() != 0) {
            novaAreaPointService.update(areaPoint);
        } else {
            novaAreaPointService.insert(areaPoint);
        }
        AreaPoint areaPoint1 = new AreaPoint();
        areaPoint1.setAreaid(areaPoint.getAreaid());
        return areaPointList(request, session, 1, areaPoint1, modelMap);
    }

    //删除
    @RequestMapping(value = "delAreaPoint")
    public ModelAndView delAreaPoint(HttpServletRequest request,
                                     HttpSession session, Integer pointid, Integer areaid,
                                     ModelMap modelMap) {
        AreaPoint areaPoint = new AreaPoint();
        areaPoint.setId(pointid);
        novaAreaPointService.delete(areaPoint);
        AreaPoint areaPoint2 = new AreaPoint();
        areaPoint2.setAreaid(areaid);
        return areaPointList(request, session, 1, areaPoint2, modelMap);
    }

    @RequestMapping(value = "existAreaPoint")
    @ResponseBody
    public Map<String, Object> existAreaPoint(AreaPoint areaPoint) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("existFlag", novaAreaPointService.existAreaPoint(areaPoint));
        return map;
    }
}
